const express = require('express');

const router = express.Router();
//导入数据库操作模块
const db = require('../db/index');

//通过商品id获取评论
router.get('/getComment', (req, res) => {
    const id = req.query.id;
    //查询评论
    db.query(`select * from comment where goods_id = ${id}`, (err, result) => {
        if (err) {
            res.json({
                code: 500,
                msg: '服务器错误'
            });
        } else {
            res.json({
                code: 200,
                msg: '查询成功',
                comments: result
            });
        }
    });
});

//浏览量增长
router.get('/viewAdd', (req, res) => {
    const id = req.query.id;
    //查询评论
    db.query(`update goods set view = view + 1 where id = ${id}`, (err, result) => {
        if (err) {
            res.json({
                code: 500,
                msg: '服务器错误'
            });
        } else {
            res.json({
                code: 200,
                msg: '成功'
            });
        }
    });
});

//获取分类列表
router.get('/getCategory', (req, res) => {
    const sql = 'SELECT * FROM category where parent_id is not null';
    db.query(sql, (err, result) => {
        if (err) {
            res.json({
                code: 500,
                msg: '服务器错误'
            });
        } else {

            //转换成对象形式
            let categoryList = {}
            result.forEach(item => {
                categoryList[item.id] = item.name
            });

            res.json({
                code: 200,
                msg: '获取成功',
                categoryList
            });
        }
    });
})

//获取两级分类列表
router.get('/reclassify', (req, res) => {
    const sql = 'SELECT * FROM category'
    db.query(sql, (err, result) => {
        if (err) {
            res.json({
                code: 500,
                message: '获取失败'
            });
        } else {
            let reclassifyList = []

            result.forEach(item => {
                //如果没有父级
                if (!item.parent_id) {
                    reclassifyList.push({
                        value: item.id,
                        label: item.name,
                        children: []
                    })
                } else {
                    reclassifyList.forEach(classify => {
                        if (classify.value == item.parent_id) {
                            classify.children.push({
                                value: item.id,
                                label: item.name
                            })
                        }
                    })
                }
            })

            res.json({
                code: 200,
                message: '获取成功',
                reclassifyList
            });
        }
    });
})

//获取轮播图
router.get('/banner', (req,res) => {
    const sql = 'select * from banner where status = 1 order by priority'

    db.query(sql,(err,result) => {
        if (err) {
            console.log(err);
            res.json({
                code: 500,
                message: '服务器错误'
            });
        } else {
            res.json({
                code: 200,
                message: '获取成功',
                banner: result
            });
        }
    })
})

//获取设置
router.get('/setting', (req,res) => {
    const sql = 'select * from setting where status = 1'

    db.query(sql,(err,result) => {
        if (err) {
            console.log(err);
            res.json({
                code: 500,
                message: '服务器错误'
            });
        } else {
            res.json({
                code: 200,
                message: '获取成功',
                setting: result[0]
            });
        }
    })
})

module.exports = router;